package net.bac.sbe.web.report.itemstoretain;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.bac.sbe.web.report.ReportFormat;
import net.bac.sbe.web.report.ReportGenerator;
import net.bac.sbe.web.report.ReportManager;
import net.sf.jasperreports.engine.JRDataSource;

public class ItemToRetainReportGenerator extends ReportGenerator {

	private static final String REPORT_DATASOURCE_KEY = "SVNDATASOURCE";
	private static final String ANALYST_NAME_KEY_FOR_ITEM_TO_RETAIN_REPORT = "analystName";
	private static final String PROJECT_NAME_KEY_FOR_ITEM_TO_RETAIN_REPORT = "projectName";

	public void generateReport(HashMap<String, Object> reportParameters){
		try {

			String projectName = (String)reportParameters.get(PROJECT_NAME_KEY);
			String projectAnalyst = (String)reportParameters.get(ANALYST_NAME_KEY);
			Map<String, List<Object>> dataset = (Map<String, List<Object>>)reportParameters.get(REPORT_DATA_SET_1);
			ReportFormat reportFormat = (ReportFormat)reportParameters.get(REPORT_FORMAT_KEY);
			String reportAbsolutePath = (String)reportParameters.get(REPORT_ABSOLUTE_PATH_KEY);
			int newItems = (Integer)reportParameters.get(NEW_ITEM_QUANTITY_KEY);
			int modifiedItems = (Integer)reportParameters.get(MODIFIED_ITEMS_QUANTITY_KEY);

			Map<String, Object> parameters = prepareParametersForInvocation(projectName, projectAnalyst, dataset, newItems, modifiedItems);
			Method method = ReportManager.class.getMethod(reportFormat.getMethodName(), String.class, String.class, Map.class);
			if(!reportAbsolutePath.endsWith(".".concat(reportFormat.getFileExtension()))){
				reportAbsolutePath = reportAbsolutePath.concat(".").concat(reportFormat.getFileExtension());
			}
			method.invoke(null, getReportTemplate(ITEMS_TO_RETAIN_REPORT), reportAbsolutePath, parameters);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}

	private Map<String, Object> prepareParametersForInvocation(
			String projectName, String projectAnalyst, Map<String, List<Object>> data, int newItems, int modifiedItems) {
		JRDataSource ds = createDataSourceFromMatrix(data);

		Map<String, Object> parameters = new HashMap<String, Object>();
		  parameters.put(PROJECT_NAME_KEY_FOR_ITEM_TO_RETAIN_REPORT, projectName);
		  parameters.put(ANALYST_NAME_KEY_FOR_ITEM_TO_RETAIN_REPORT, projectAnalyst);
		  parameters.put(REPORT_DATASOURCE_KEY, ds);
		  parameters.put("itemQuantity", String.valueOf(newItems + modifiedItems));
		  parameters.put("modItemCount", String.valueOf(modifiedItems));
		  parameters.put("newItemCount", String.valueOf(newItems));
		return parameters;
	}
}
